package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.foxcard.FoxSaleCard;
import com.yanqu.road.entity.activity.foxcard.UserFoxCardMonster;
import com.yanqu.road.entity.activity.foxcard.UserFoxCardShop;
import org.apache.mina.util.CopyOnWriteMap;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserFoxCardMonsterDaoImpl extends TempDataObjectDao<UserFoxCardMonster> {

    public static String getUserFoxCardMonsterSql = "select * from `t_u_activity_fox_monster` where `activityId`=? and `userId`=?";
    public Map<Integer, UserFoxCardMonster> getUserFoxCardMonster(int activityId,long userId) {
        Map<Integer, UserFoxCardMonster> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserFoxCardMonsterSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserFoxCardMonster data = parse(rs);
                dataMap.put(data.getRound(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String updateUserFoxCardMonsterSql = "update `t_u_activity_fox_monster` set skipEffect=?,bossLimitEffect=?,hp=? where activityId=? and userId=? and round=?";
    @Override
    protected boolean updateData(UserFoxCardMonster userFoxCardMonster) {
        userFoxCardMonster.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardMonster.getSkipEffect()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardMonster.getBossLimitEffect()));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardMonster.getHp().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardMonster.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardMonster.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardMonster.getRound()));
        if (getDb().executeNoneQuery(updateUserFoxCardMonsterSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserFoxCardMonsterSql = "insert into `t_u_activity_fox_monster`(activityId,userId,round,skipEffect,bossLimitEffect,hp) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserFoxCardMonster userFoxCardMonster) {
        userFoxCardMonster.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardMonster.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardMonster.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardMonster.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardMonster.getSkipEffect()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardMonster.getBossLimitEffect()));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardMonster.getHp().toString()));
        if (getDb().executeNoneQuery(insertUserFoxCardMonsterSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserFoxCardMonster parseData(ResultSet rs) throws SQLException {
        UserFoxCardMonster userFoxCardMonster = new UserFoxCardMonster();
        userFoxCardMonster.setActivityId(rs.getInt("activityId"));
        userFoxCardMonster.setUserId(rs.getLong("userId"));
        userFoxCardMonster.setRound(rs.getInt("round"));
        userFoxCardMonster.setSkipEffect(rs.getInt("skipEffect"));
        userFoxCardMonster.setBossLimitEffect(rs.getInt("bossLimitEffect"));
        userFoxCardMonster.setHp(new BigInteger(rs.getString("hp")));

        return userFoxCardMonster;
    }
}
